<template>
  <el-card>
    <template #header>
      <span>注册用户</span>
    </template>
    <el-form v-loading="loading" :model="form" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-radio v-model="form.gender" label="m">男</el-radio>
        <el-radio v-model="form.gender" label="f">女</el-radio>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="regist()">立即创建</el-button>
        <el-button @click="back">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  //验证用户名重复
  data() {
    const _this = this;
    async function isUsernameSame(rule, value, cb) {
      const { data } = await _this.axios({
        method: "get",
        url: `/users/exists/username/${value}`
      });
      if (data) {
        cb(new Error("用户名重复"));
      } else {
        cb();
      }
    }
    //邮箱重复

    async function isEmailSame(rule, value, cb) {
      const { data } = await _this.axios({
        method: "get",
        url: `/users/exists/email/${value}`
      });
      if (data) {
        cb(new Error("邮箱重复"));
      } else {
        cb();
      }
    }
    //手机重复

    async function isPhoneSame(rule, value, cb) {
      const { data } = await _this.axios({
        method: "get",
        url: `/users/exists/phone/${value}`
      });
      if (data) {
        cb(new Error("手机重复"));
      } else {
        cb();
      }
    }

    return {
      loading: false,
      form: {
        username: "",
        password: "",
        gender: "m",
        email: "",
        phone: "",
        confirmPassword: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名必须填写",
            trigger: "blur"
          },
          {
            min: 4,
            max: 11,
            message: "长度在 4 到 11 个字符",
            trigger: "blur"
          },
          {
            validator: isUsernameSame
          }
        ],
        password: [
          {
            required: true,
            message: "密码必须填写",
            trigger: "blur"
          },
          { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" }
        ],
        //自定义规则
        confirmPassword: [
          {
            required: true,
            validator: (rule, value, cb) => {
              if (value == this.form.password) {
                cb();
              } else {
                cb(new Error("两次密码不一样"));
              }
            }
          }
        ],
        email: [
          {
            required: true,
            message: "邮箱必须填写",
            trigger: "blur"
          },
          { type: "email", message: "邮箱格式错误！" },
          {
            validator: isEmailSame
          }
        ],
        phone: [
          {
            required: true,
            validator: (rule, value, cb) => {
              if (/^1[34589]\d{9}$/.test(value)) {
                cb();
              } else {
                cb(new Error("手机格式错误"));
              }
            }
          },
          { validator: isPhoneSame }
        ]
      }
    };
  },
  methods: {
    async regist() {
      await this.$refs.form.validate(flag => {
        if (!flag) {
          return;
        }
        this.loading = true;
        this.axios({
          method: "post",
          url: "/users/regist",
          data: {
            username: this.form.username,
            email: this.form.email,
            phone: this.form.phone,
            password: this.form.password,
            gender: this.form.gender
          }
        });
        this.loading = false;

        this.$message.success("添加成功");
        this.$router.push({
          name: "login"
        });
      });
    },
    back() {
      this.$router.push({
        name: "login"
      });
    }
  }
};
</script>

